package com.dao;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

abstract class AbstractSQL {
//	protected static final String DBRIVER = "com.mysql.jdbc.Driver";
//	protected static final String DBURL = "jdbc:mysql://localhost:3306/isdoub";
//	protected static final String DBUSER = "root";
//	protected static final String DBPASS = "123";
	private String jndiName = "java:/comp/env/jdbc/demoDB";
//	 static {
//		try {
//			Class.forName(DBRIVER);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	 
	 protected Connection getConnection()  {  
	        DataSource ds = null;
			try {
				Context initContext = new InitialContext();
				Context envContext  = (Context)initContext.lookup("java:/comp/env");
				ds = (DataSource) envContext.lookup("jdbc/demoDB");
			} catch (NamingException e) {
				e.printStackTrace();
			}  
			 
	        if ( ds == null ) {  
	           try {
				throw new Exception("Data source not found!");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
	        }  
	        try {
				return ds.getConnection();
			} catch (SQLException e) {
				e.printStackTrace();
				return null;
			}  
	    }  
}
